package com.puyuba.puyuba.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.puyuba.puyuba.SkinApplication;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.commonutil.ToastHelper;

import java.util.List;

public class MyJavascriptInterface {

    private Context mContext;
    public MyJavascriptInterface(Context context){
        this.mContext = context;
    }

    @JavascriptInterface
    public String getToken() {
        Log.i("inffs", "getToken : " + CacheUserInfo.getInstance().getToken());
        return CacheUserInfo.getInstance().getToken();
    }

    @JavascriptInterface
    public int getUserid() {
        Log.i("inffs", "getId: " + CacheUserInfo.getInstance().getId());
        return CacheUserInfo.getInstance().getId();
    }

    @JavascriptInterface
    public void call(String phone){
        //拨打老师电话
        XXPermissions.with(mContext)
                // 申请单个打电话权限
                .permission(Permission.CALL_PHONE)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            //toast("获取录音和日历权限成功");
                            callPhone(phone);
                        } else {
                            //"获取部分权限成功，但部分权限未正常授予");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(SkinApplication.getInstance(), permissions);
                        } else {
                            //toast("获取录音和日历权限失败");
                        }
                    }
                });
    }

    public void callPhone(String phone) {
        if (phone == null) {
            ToastHelper.show("请重新获取数据");
            return;
        }

        Intent Intent = new Intent(android.content.Intent.ACTION_DIAL,
                Uri.parse("tel:" + phone));//跳转到拨号界面，同时传递电话号码
        mContext.startActivity(Intent);
    }
}
